package cc.yiueil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/")
public class CookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String lang = req.getParameter("lang");
        if (lang != null) {
            Cookie cookie = new Cookie("lang", lang);
            // 指定Cookie的生效范围 / 代表当前域名的所有请求
            cookie.setDomain(req.getServerName());
            cookie.setPath("/");
            cookie.setSecure(false);
            cookie.setMaxAge(3600);
            // 把cookie添加到响应
            resp.addCookie(cookie);
        }
        resp.sendRedirect(req.getContextPath() + "/main.jsp");
    }
}
